package com.token.iot.bratterycharging.gateway.business;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.OperExchanger;
import com.token.iot.bratterycharging.gateway.api.Constant;
import com.token.iot.bratterycharging.gateway.api.Port;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;

/**
 * 远程停止充电
 * 
 * @author chenzhixuan
 *
 */
@Component("RTNDuplexProcessor")
public class RTNDuplexProcessor extends DuplexProcessor {

	@Override
	public String request(String imei, Map<String, String> params) throws PendingException {
		String c = "RTN";
		String sessionId = IDGeneration.createSeesionID();
		String command = null;

		command = c + sessionId + "/";
		String port = params.get(Constant.PORT);
		command = command + port;
		loger.info("the send =" + new String(command));
		byte[] cc = ByteUtil.addFlag(command.getBytes());

		// 进行响应码转换
		OperExchanger.register("RTN", "DCH");
		context.writeAndFlush(cc);
		return sessionId;

	}

	@Override
	public Port response(String response) throws PendingException {
		String leftStr = response.substring(14);
		String[] params = leftStr.split("#/#");
		Port port = new Port();
		port.setNo(params[0]);
		int time =0;
		try {
			 time = Integer.parseInt(params[1]);
		}catch(Exception e) {
			loger.error(e);
		}
		port.setTime(time);

		return port;
	}

}
